package com.lqjava.flowable.serverapp.app.api;

import cn.hutool.core.date.DateUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import org.flowable.engine.*;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.flowable.task.api.TaskQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/open/test")
@RestController
public class TestApi {
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private ProcessEngine processEngine;



    @GetMapping("xx")
    public String start(){
        final String processInstanceKey = "manyUser";
        HashMap<String, Object> map = new HashMap<>();
        map.put("userid", "123");
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processInstanceKey, map);

        List<Task> tasks = taskService.createTaskQuery().taskCandidateUser("平台").list();
        for (Task task : tasks) {
            String taskId = task.getId();
            Map<String, Object> variables = taskService.getVariables(taskId);
            Map<String, Object> variablesLocal = taskService.getVariablesLocal(taskId);

            System.out.println(task.toString());
            System.out.println(variables);
            System.out.println(variablesLocal);
        }


        return "提交成功.流程Id为：" + processInstance.getId();
    }

    @GetMapping("start")
    public String start(Integer day,Integer status){
        final String processInstanceKey = "leave";
        final String userid = "001";


        HashMap<String, Object> map = new HashMap<>();
        map.put("userid", userid);
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processInstanceKey, map);




        return "提交成功.流程Id为：" + processInstance.getId();
    }


    @GetMapping("list")
    public String list(String userid){
        List<Task> tasks = taskService.createTaskQuery().taskAssignee(userid).orderByTaskCreateTime().desc().list();
        for (Task task : tasks) {
            String taskId = task.getId();
            Map<String, Object> variables = taskService.getVariables(taskId);
            Map<String, Object> variablesLocal = taskService.getVariablesLocal(taskId);

            System.out.println(task.toString());
            System.out.println(variables);
            System.out.println(variablesLocal);
        }
        return tasks.size() == 0 ? "-1" : tasks.get(0).getId();
    }

    @GetMapping("complete")
    public String audit(String taskId,Integer day,Integer status){
        final String parentUserid = "002";
        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
        if (task == null) {
            throw new RuntimeException("流程不存在");
        }
        //通过审核
        HashMap<String, Object> map = new HashMap<>();
        map.put("day", day);
        map.put("status", status);
        map.put("parentUserid",parentUserid);
        taskService.complete(taskId, map);
        return DateUtil.now();
    }


    public static void main(String[] args) {

        final String url = "http://192.168.6.229/K3Cloud/Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc";
        final String content = "{\"acctID\":\"60753ac0ff2a00\",\"username\":\"郑廷俊\",\"password\":\"666666\",\"lcid\":\"2052\"}";

        RestTemplate restTemplate = new RestTemplate();


        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);

        HttpEntity<String> request = new HttpEntity<>(content, headers);

        ResponseEntity<String> postForEntity = restTemplate.postForEntity(url, request, String.class);

        String body = postForEntity.getBody();

        System.out.println(body);
    }


}
